package org.jeecg.modules.demo.officialaccounttoken.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.officialaccounttoken.entity.OfficialAccountToken;
import org.jeecg.modules.demo.officialaccounttoken.mapper.OfficialAccountTokenMapper;
import org.jeecg.modules.demo.officialaccounttoken.service.IOfficialAccountTokenService;
import org.jeecg.modules.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class OfficialAccountTokenServiceImpl extends ServiceImpl<OfficialAccountTokenMapper, OfficialAccountToken> implements IOfficialAccountTokenService {
    @Autowired
    private OfficialAccountTokenMapper officialAccountTokenMapper;
    private OfficialAccountToken mToken;
    private String mAppID;
    private String mAppSceret;

    // 根据订单id查询
    @Override
    public OfficialAccountToken getByAppId(String appId, String appSecret, Long currentTime) {
        return officialAccountTokenMapper.getByAppId(appId, appSecret, currentTime);
    }

    /**
     * 通过参数获取Token信息
     *
     * @param appID
     * @param appSceret
     * @return
     */
    public OfficialAccountToken getToken(String appID, String appSceret) {
        mAppID = appID;
        mAppSceret = appSceret;
        mToken = new OfficialAccountToken();

        // 1.确定是否要更新token，无需更新则直接直接返回获取的token
        //if (updateToken())
        //{
        //    return mToken;
        //}

        // 2. 如需更新
        if (!getTokenbyhttps(mAppID, mAppSceret)) {
            System.out.println("获取失败！");
            return null;
        }

        return mToken;
    }

    /**
     * 通过https请求获取token
     *
     * @param appID
     * @param appSceret
     * @return
     */
    private boolean getTokenbyhttps(String appID, String appSceret) {
        Date create_time = new Date();
        Long token_time = create_time.getTime();

        try {
            // 请求地址
            String path = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                    + appID + "&secret=" + appSceret;

            String strResp = HttpUtils.sendGet(path);
            System.out.println(strResp);

            // 解析获取的token信息
            Map<String, Object> tMap = JSON.parseObject(strResp, Map.class);

            System.out.println(tMap.toString());

            mToken.setToken((String) tMap.get("access_token"));
            //mToken.setExpiresIn(Integer.parseInt((String) tMap.get("expires_in")));
            mToken.setExpiresIn(Integer.parseInt(tMap.get("expires_in").toString()));
            mToken.setAppId(appID);
            mToken.setAppSecret(appSceret);
            mToken.setTokenTime(token_time);
            mToken.setCreateTime(create_time);
            //save(mToken);

            System.out.println(mToken.getToken());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 获取Token信息
     *
     * @return
     */
    private boolean updateToken() {
        Long current_time = new Date().getTime();
        OfficialAccountToken officialAccountToken = getByAppId(mAppID, mAppSceret, current_time);
        if (officialAccountToken != null) {
            mToken.setId(officialAccountToken.getId());
            mToken.setToken(officialAccountToken.getToken());
            mToken.setExpiresIn(officialAccountToken.getExpiresIn());
            mToken.setAppId(officialAccountToken.getAppId());
            mToken.setAppSecret(officialAccountToken.getAppSecret());

            return true;
        } else {
            return false;
        }
    }

}
